<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 分类树 -->
      <el-col :span="5" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>报表分类</span>
          </div>
          <div class="category-tree">
            <el-tree
              :data="categoryTree"
              :props="defaultProps"
              :expand-on-click-node="false"
              @node-click="handleCategoryClick"
              highlight-current
            >
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
              </span>
            </el-tree>
          </div>
        </el-card>
      </el-col>
      
      <!-- 报表列表 -->
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="报表名称" prop="reportName">
            <el-input
              v-model="queryParams.reportName"
              placeholder="请输入报表名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="报表类型" prop="reportType">
            <el-select v-model="queryParams.reportType" placeholder="请选择报表类型" clearable size="small">
              <el-option
                v-for="dict in dict.type.report_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="reportStatus">
            <el-select v-model="queryParams.reportStatus" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in dict.type.common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['rpt:definition:create']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['rpt:definition:update']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['rpt:definition:delete']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['rpt:definition:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="报表ID" align="center" prop="reportId" v-if="false" />
          <el-table-column label="报表编码" align="center" prop="reportCode" width="120" />
          <el-table-column label="报表名称" align="center" prop="reportName" :show-overflow-tooltip="true" width="180" />
          <el-table-column label="报表类型" align="center" prop="reportType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.report_type" :value="scope.row.reportType"/>
            </template>
          </el-table-column>
          <el-table-column label="数据源" align="center" prop="dataSource" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="reportStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.common_status" :value="scope.row.reportStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                v-hasPermi="['rpt:definition:query']"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleDesign(scope.row)"
                v-hasPermi="['rpt:design:edit']"
              >设计</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['rpt:definition:update']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['rpt:definition:delete']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listReport, getReport, delReport, exportReport } from "@/api/report/report";
import { getCategoryTree, getReportByCategory } from "@/api/report/category";

export default {
  name: "ReportList",
  dicts: ['report_type', 'common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报表表格数据
      reportList: [],
      // 报表分类树数据
      categoryTree: [],
      // 当前选中的分类ID
      currentCategoryId: null,
      // 树形控件配置
      defaultProps: {
        children: "children",
        label: "categoryName"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportName: undefined,
        reportType: undefined,
        reportStatus: undefined,
        categoryId: undefined
      }
    };
  },
  created() {
    this.getCategoryTree();
    this.getList();
  },
  methods: {
    /** 查询报表分类树 */
    getCategoryTree() {
      getCategoryTree().then(response => {
        // 添加一个"全部"节点
        const allNode = {
          categoryId: 0,
          categoryName: "全部分类",
          children: []
        };
        this.categoryTree = [allNode].concat(response.data);
      });
    },
    
    /** 处理分类点击事件 */
    handleCategoryClick(data) {
      this.currentCategoryId = data.categoryId;
      this.queryParams.categoryId = data.categoryId === 0 ? undefined : data.categoryId;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    
    /** 查询报表列表 */
    getList() {
      this.loading = true;
      // 如果选择了分类，使用分类API获取报表列表
      if (this.currentCategoryId && this.currentCategoryId !== 0) {
        getReportByCategory(this.currentCategoryId).then(response => {
          this.reportList = response.rows || [];
          this.total = response.total || 0;
          this.loading = false;
        });
      } else {
        // 否则使用常规API
        listReport(this.queryParams).then(response => {
          this.reportList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    
    // 表单重置
    reset() {
      this.form = {
        reportId: undefined,
        reportCode: undefined,
        reportName: undefined,
        reportType: "0",
        dataSource: undefined,
        reportStatus: "0",
        permissionCode: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // 重置时保留分类选择
      this.queryParams.categoryId = this.currentCategoryId === 0 ? undefined : this.currentCategoryId;
      this.handleQuery();
    },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/report/design/add' });
    },
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      const reportId = row.reportId || this.ids[0];
      this.$router.push({ path: `/report/design/edit/${reportId}` });
    },
    
    /** 设计按钮操作 */
    handleDesign(row) {
      const reportId = row.reportId;
      this.$router.push({ path: `/report/design?reportId=${reportId}` });
    },
    
    /** 查看报表 */
    handleView(row) {
      const reportId = row.reportId;
      this.$router.push({ path: `/report/view/${reportId}` });
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除报表编号为"' + reportIds + '"的数据项?').then(function() {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel('/ldzl-rpt/rpt/definition/export', this.queryParams);
    }
  }
};
</script>

<style scoped>
.category-tree {
  height: 100%;
  overflow: auto;
}
.box-card {
  margin-bottom: 10px;
  height: calc(100vh - 200px);
}
</style> 